from game import zz_game
from zz import zz_log, zz_img
import time

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "淘宝农场_钩鱼"

    def game_alert(self):
        self.alert_close_by_file("./game/img/淘宝农场_钩鱼")

    def play_task(self, timeout=15):
        self.log_msg(f"已进入浏览页面，开始浏览{timeout}秒")

        deadline = time.time() + timeout
        while True:
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_up()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)
            self.swipe_down()
            self.d.sleep(1.5)
            if time.time() > deadline:
                self.log_msg("浏览完成")
                break
        self.d.press("back")

    def do_prize(self):
        self.log_msg("领取奖励")
        self.swipe_up()
        swipe_count = 0
        while True:
            if not self.d.xpath('//android.widget.Button[@text="领取"]').wait(2):
                if swipe_count > 0:
                    self.log_msg("领取奖励完成")
                    break
                self.swipe_up()
                swipe_count += 1
                continue
            self.log_msg("点击领取")
            self.d.xpath('//android.widget.Button[@text="领取"]').click(2)
            self.log_msg("点击关闭弹窗")
            self.img_click('./game/img/淘宝农场_钩鱼/alert_close.png')
            swipe_count = 0

        self.img_click_if_exists('点击关闭任务列表', './game/img/淘宝农场_钩鱼/task_close.png', './game/img/淘宝农场_钩鱼/diao_00.png')

    def diao(self):
        self.log_msg("开始钓鱼，获取钩鱼按钮位置")
        r = self.find_img('./game/img/淘宝农场_钩鱼/diao_00.png')
        if r.err():
            self.log_msg("获取钩鱼按钮位置-失败")
            return
        x, y = r.get_data("center")

        while True:
            if self.img_exit('./game/img/淘宝农场_钩鱼/diao_prize.png', 1):
                self.log_msg("存在钓鱼奖励，点击领取")
                self.img_click('./game/img/淘宝农场_钩鱼/diao_prize.png')
                if self.img_exit('./game/img/淘宝农场_钩鱼/diao_alert_task.png', 2):
                    self.log_msg("可以浏览加倍，点击浏览")
                    self.img_click('./game/img/淘宝农场_钩鱼/diao_alert_task.png')
                    self.play_task(16)
            if self.img_exit('./game/img/淘宝农场_钩鱼/diao_prize_0.png', 1, 0.9):
                self.log_msg("钓鱼次数已用光")
                break

            if self.img_exit('./game/img/淘宝农场_钩鱼/play_main.png', 1):
                self.log_msg("打开了任务列表，点击关闭")
                self.img_click('./game/img/淘宝农场_钩鱼/task_close.png')
                break

            self.log_msg(f"点击钓鱼{(x, y)}", False)
            self.d.click(x, y)
            self.d.sleep(1)
            # self.img_click('./game/img/淘宝农场_钩鱼/diao_00.png')
            if self.img_exit('./game/img/淘宝农场_钩鱼/diao_boss.png', 2):
                self.log_msg("出现大鱼，疯狂点25下")
                self.img_click('./game/img/淘宝农场_钩鱼/diao_boss.png', 5, 25)

            if self.img_exit('./game/img/淘宝农场_钩鱼/diao_alert.png', 2):
                self.log_msg("出现成功弹窗")
                if self.img_exit('./game/img/淘宝农场_钩鱼/diao_alert_task.png', 2):
                    self.log_msg("可以浏览加倍，点击浏览")
                    self.img_click('./game/img/淘宝农场_钩鱼/diao_alert_task.png', 2)
                    self.play_task(16)
                else:
                    self.log_msg("点击关闭弹窗")
                    self.img_click('./game/img/淘宝农场_钩鱼/alert_close.png')

            if self.img_exit('./game/img/淘宝农场_钩鱼/alert_close.png', 1):
                self.log_msg("存在关闭弹窗，点击关闭弹窗")
                self.img_click('./game/img/淘宝农场_钩鱼/alert_close.png')

            self.d.sleep(1)

    def handle_main(self):
        self.log_msg("等待进入钩鱼主界面")
        self.img_exit('./game/img/淘宝农场_钩鱼/game_main.png', 30)
        self.log_msg("已进入钩鱼主界面")

        if self.img_exit('./game/img/淘宝农场_钩鱼/prize_00.png', 2):
            self.log_msg("未领取每日宝箱，点击领取")
            self.img_click('./game/img/淘宝农场_钩鱼/prize_00.png')
            self.log_msg("点击开启宝箱")
            self.img_click('./game/img/淘宝农场_钩鱼/prize_01.png')

        while self.img_exit('./game/img/淘宝农场_钩鱼/alert_close.png', 1):
            self.log_msg("点击关闭弹窗")
            self.img_click('./game/img/淘宝农场_钩鱼/alert_close.png')

        self.img_click_if_exists("打开任务列表", './game/img/淘宝农场_钩鱼/play_01.png', './game/img/淘宝农场_钩鱼/play_main.png')
        if self.d.xpath('//android.widget.Button[@text="分享"]').wait(2):
            self.log_msg("存在分享任务，点击分享")
            self.xpath_click('//android.widget.Button[@text="分享"]')
            self.d.sleep(2)
            self.log_msg("点击分享给Ta们")
            self.img_click('./game/img/淘宝农场_钩鱼/play_share.png')

        swipe_count = 0
        while True:
            if not self.d.xpath('//*[@text="去逛逛"]').wait(2):
                if swipe_count > 0:
                    self.log_msg("浏览任务已做完")
                    break
                self.swipe_up()
                swipe_count += 1
                continue

            self.log_msg("点击进入浏览任务")
            self.xpath_click('//*[@text="去逛逛"]')
            self.play_task()
            swipe_count = 0

        # 领取奖励
        self.do_prize()
        # 钩鱼
        self.diao()

        self.img_click_if_exists("打开任务列表", './game/img/淘宝农场_钩鱼/play_01.png', './game/img/淘宝农场_钩鱼/play_main.png')
        # 再领取奖励
        self.do_prize()
        # 再钩鱼
        self.diao()

        self.log_msg("点击退出钩鱼")
        self.close_game()

        return True
